module executor.loading.LoadingExecutor;

import msgtrans;
import message.Constants;
import message.MsgDefine;
import google.protobuf;
import hunt.logging;
import std.array;
import std.conv : to;
import hunt.util.Serialize;
import client.GateServerClient;
import client.MsgServerClient;
import service.ContactsService;
import core.stdc.string;

/**
用户登录加载通讯录模块
**/

@TransportServer(LogicServerName)
class LoadingExecutor : AbstractExecutor!(LoadingExecutor) {

  @MessageId(MESSAGE.CMD_CONTACTS_REQ)
  void contacts(TransportContext ctx, MessageBuffer buffer) {
    auto req = new InitMsg();
    buffer.data.fromProtobuf!InitMsg(req);
    //ulong realId = (req.clientId >> 16);
    ServerExtend serverExtend;
    memcpy(&serverExtend,buffer.extend.ptr,ServerExtend.sizeof);

    // serverExtend.userId : userid + src  右位偏移2个字节取出真实的userId
    ulong realId = (serverExtend.userId >> 16);
    logInfo("CMD_CONTACTS_REQ : %d" , realId);
    ContactRES res =  ContactsService.getContact(realId,req.timestamp);
    res.clientId = req.clientId;
    //Extend extend;
    //extend.userId = buffer.extend.userId;
    ubyte[] extend = buffer.extend;
    ctx.session.send(new MessageBuffer(MESSAGE.CMD_CONTACTS_RES,res.toProtobuf.array,extend));
  }
}

